package com.example.hrandroidapp.components;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hrandroidapp.R;
import com.example.hrandroidapp.common.StringUtil;
import com.example.hrandroidapp.common.UIUtil;

public class ActionsLinearLayout extends LinearLayout {


    /** List of {@link ActionItem}s, the action bar will draw the layout based this. */
    private List<ActionItem> actionItems;

    /** Whether to draw the divider. */
    private boolean drawDivider = true;

    /** The color for the whole bar background. */
    private Integer barColor;

    /** The color of the label */
    private Integer labelColor;
    
    private EditText etComment;

    /** Whether the font style of label is bold */
    private boolean labelBold;

    public String getLeaveCommment() {
    	return etComment.getText().toString();
    }
    
    public void setLabelBold(boolean labelBold) {
        this.labelBold = labelBold;
    }

    public void setBarColor(Integer barColor) {
        this.barColor = barColor;
    }

    public void setLabelColor(Integer labelColor) {
        this.labelColor = labelColor;
    }

    public ActionsLinearLayout(Context context) {
        super(context);
    }

    public ActionsLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public List<ActionItem> getActionItems() {
        return actionItems;
    }

    /**
     * @param actionItems {@link #actionItems}
     */
    public void setActionItems(List<ActionItem> actionItems) {
        this.actionItems = actionItems;
    }

    /**
     * Clear all the action items if not empty.
     */
    public void clear() {
        if (actionItems != null) {
            actionItems.clear();
        }
    }

    /**
     * Add an ActionItem.
     */
    public void addActionItem(ActionItem actionItem) {
        if (actionItems == null) {
            actionItems = new ArrayList<ActionItem>();
        }

        if (actionItem != null) {
            actionItems.add(actionItem);
        }
    }

    /**
     * Draw the action bar by action items. It will clear the action bar first and draw the whole layout again. So
     * before the call, make sure you already set the list of ActionItem.
     */
    public void drawActionBar() {
        // Clear the action bar first.
        removeAllViews();

        // Loop the action items and draw each action button.
        if (actionItems != null && actionItems.size() != 0) {

            // Filter and get all the visible items.
            List<ActionItem> itemsVisible = new ArrayList<ActionItem>();
            for (ActionItem item : actionItems) {
                if (item.visible) {
                    itemsVisible.add(item);
                }
            }
            
            this.setOrientation(LinearLayout.VERTICAL);

            // Set gravity
            setGravity(Gravity.CENTER);

            // Set minimum height
            setMinimumHeight((int) UIUtil.getPxFromDp(getContext(), 45));

            // Set background
            if (barColor == null) {
                setBackgroundColor(getResources().getColor(R.color.black));
            } else {
                setBackgroundColor(barColor);
            }
            LayoutInflater inflater = LayoutInflater.from(getContext());

            // Calculate the width for each action button and make sure each action button has the same size.
            WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int width = (display.getWidth() - itemsVisible.size() - 1) / itemsVisible.size();
            
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 5, 0, 50);
            
            etComment = (EditText) inflater.inflate(R.layout.leave_comment, this, false); 
            etComment.setBackground(getResources().getDrawable(R.drawable.boundary_box_grey));
            etComment.setLayoutParams(params);
            etComment.setHint("Your Reason"); 
            addView(etComment); 
            
            LinearLayout container = (LinearLayout) inflater.inflate(R.layout.leave_approve_container, this, false);
            container.setGravity(Gravity.CENTER);
            // Draw each action item.
            for (int n = 0; n < itemsVisible.size(); n++) {
                ActionItem item = itemsVisible.get(n);

                // Inflate view
                LinearLayout itemLayout = (LinearLayout) inflater.inflate(R.layout.actions_linearlayout_item, this, false);

                // Set width
                itemLayout.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT));

                // OnClickListener
                itemLayout.setOnClickListener(item.onClickListener);
                
                // Icon
                ImageView imageViewIcon = (ImageView) itemLayout.findViewById(R.id.imageView_icon);
                if (item.iconResource == null) {
                    imageViewIcon.setVisibility(View.GONE);
                } else {
                    imageViewIcon.setImageResource(item.iconResource);
                }

                // Label
                TextView textViewLabel = (TextView) itemLayout.findViewById(R.id.textView_label);
                textViewLabel.setGravity(Gravity.CENTER_VERTICAL);
                
                textViewLabel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
                
                if (StringUtil.isNullOrEmpty(item.label)) {
                    textViewLabel.setVisibility(View.GONE);
                } else {
                    textViewLabel.setText(item.label);
                    if(item.label.equals(getResources().getString(R.string.btn_approve)))
                    	textViewLabel.setCompoundDrawablesWithIntrinsicBounds(R.drawable.checkmark_light_l, 0, 0, 0);
                    else
                    	textViewLabel.setCompoundDrawablesWithIntrinsicBounds(R.drawable.cancel_light_l, 0, 0, 0);
                }
                if (labelColor != null) {
                    textViewLabel.setTextColor(labelColor);
                }
                if (labelBold) {
                    textViewLabel.setTypeface(null, Typeface.BOLD);
                }

                // Add the vertical divider
                if (n >= 1 && drawDivider) {
                    inflater.inflate(R.layout.actions_linearlayout_divider, container, true);
                }

                // Add to the action bar
//                addView(itemLayout);
                container.addView(itemLayout); 
            }
           addView(container); 
        }
    }

    /**
     * @param drawDivider {@link #drawDivider}
     */
    public void setDrawDivider(boolean drawDivider) {
        this.drawDivider = drawDivider;
    }

    /**
     * Represent each action item in the action bar. Usually it has an icon, a label and a OnClickListener for the click
     * action.
     */
    public static class ActionItem {

        /** The icon resource, if null the icon will ge gone. */
        private Integer iconResource;

        /** The label text of the action */
        private String label;

        /** True, the whole action item will be visible, false will be View.GONE. */
        private boolean visible;

        /** Click listener for clicking the action item. */
        private View.OnClickListener onClickListener;

        public ActionItem(Integer iconResource, String label, boolean visible, View.OnClickListener onClickListener) {
            super();
            this.iconResource = iconResource;
            this.label = label;
            this.visible = visible;
            this.onClickListener = onClickListener;
        }

        public Integer getIconResource() {
            return iconResource;
        }

        public void setIconResource(Integer iconResource) {
            this.iconResource = iconResource;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public boolean isVisible() {
            return visible;
        }

        public void setVisible(boolean visible) {
            this.visible = visible;
        }

        public View.OnClickListener getOnClickListener() {
            return onClickListener;
        }

        public void setOnClickListener(View.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

    }


}
